package zhaoyong.wang.tlias.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import zhaoyong.wang.tlias.pojo.Emp;
import zhaoyong.wang.tlias.pojo.Result;
import zhaoyong.wang.tlias.service.impl.EmpService;
import zhaoyong.wang.tlias.service.impl.EmpServicelmpl;
import zhaoyong.wang.tlias.utils.jwtUtils;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpServicelmpl empServicelmpl;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp){
        log.info("员工登录：{}",emp);
       Emp emp1 = empServicelmpl.login(emp);
       if (emp1 != null){
           Map<String,Object> claims = new HashMap<>();
           claims.put("id",emp1.getId());
           claims.put("name",emp1.getName());
           claims.put("username",emp1.getUsername());
          String jwt = jwtUtils.generateJWT(claims);
          return Result.success(jwt);
       }
        return Result.error("用户名或密码错误");
    }
}
